//
//  LS_FindProtocolImp.m
//  breadPro
//
//  Created by 刘文超 on 2019/7/5.
//  Copyright © 2019 刘文超. All rights reserved.
//

#import "LS_FindProtocolImp.h"
#import "LS_ActivitysModel.h"
#import "LS_SoreItemModel.h"
@interface LS_FindProtocolImp()
/**
 轮播图数据
 */
@property(nonatomic,strong)NSMutableArray *banner;

/**
 跑马灯数组
 */
@property(nonatomic,strong)NSMutableArray *news;

/**
 分类数组
 */
@property(nonatomic,strong)NSMutableArray *soreItems;

/**
 广告数据
 */
@property(nonatomic,strong)NSMutableArray *advitiseItems;

/**
 发现模块数据
 */
@property(nonatomic,strong)NSMutableDictionary *foundDataDic;
@end

@implementation LS_FindProtocolImp

- (RACSignal *)ls_requestDataWithUrl:(NSString *)url WithParam:(NSDictionary *)param
{
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
       
        HTURLSessionTask *task = [HTNetWorking getWithUrl:url refreshCache:YES showHUD:@"加载中..." params:param  success:^(id response) {
            [self.banner removeAllObjects];
            [self.news removeAllObjects];
            [self.soreItems removeAllObjects];
            [self.advitiseItems removeAllObjects];
            for (NSDictionary *dict in response[@"data"][@"activitys"]) {
                LS_ActivitysModel *model = [LS_ActivitysModel mj_objectWithKeyValues:dict];
                [self.advitiseItems addObject:model];
            }
            for (NSDictionary *dict in response[@"data"][@"ad"]) {
                [self.banner addObject:dict];
            }
            for (NSDictionary *dict in response[@"data"][@"news"]) {
                [self.news addObject:dict];
            }
            for (NSDictionary *dict in response[@"data"][@"list"]) {
                LS_SoreItemModel *model = [LS_SoreItemModel mj_objectWithKeyValues:dict];
                [self.soreItems addObject:model];
            }
            [self.foundDataDic setValue:self.banner forKey:FoundBannerkey];
            [self.foundDataDic setValue:self.news forKey:FoundNewskey];
            [self.foundDataDic setValue:self.soreItems forKey:FoundSorekey];
            [self.foundDataDic setValue:self.advitiseItems forKey:FoundActivitykey];
            //图片计算高度
            NSMutableDictionary *d = @{}.mutableCopy;
            UIImage *lwc_image =[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:response[@"data"][@"add_activity_pic"]]]];
            [d setObject:response[@"data"][@"add_activity_pic"] forKey:@"imgurl"];
            [d setObject:lwc_image == nil ? @"80" : kFORMAT(@"%.2f",lwc_image.size.height/lwc_image.size.width*(ScreenWidth-20)) forKey:@"imgheight"];
            [self.foundDataDic setValue:d forKey:FoundAdd_activity_pic];
            [subscriber sendNext:self.foundDataDic];
            [subscriber sendCompleted];
            NSLog(@"data--------%@",response);
        } fail:^(NSError *error) {
            [subscriber sendNext:error];
        }];
        return [RACDisposable disposableWithBlock:^{
            [task cancel];
        }];
    }];
}
-(RACSignal *)ls_praiseZanWithUrl:(NSString *)url WithParam:(NSDictionary *)param
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
      HTURLSessionTask *task = [HTNetWorking getWithUrl:url refreshCache:YES  params:param success:^(id response) {
          NSLog(@"------------%@",response);
          
          [subscriber sendNext:response];
          [subscriber sendCompleted];
        } fail:^(NSError *error) {
            [subscriber sendNext:error];
        }];
        
        return [RACDisposable disposableWithBlock:^{
            [task cancel];
        }];
        
    }];
   
}


-(NSMutableArray *)banner
{
    return LS_LAZY(_banner, @[].mutableCopy);
}
-(NSMutableArray *)news
{
    return LS_LAZY(_news, @[].mutableCopy);
}
-(NSMutableArray *)soreItems
{
    return LS_LAZY(_soreItems, @[].mutableCopy);
}
-(NSMutableArray *)advitiseItems
{
    return LS_LAZY(_advitiseItems, @[].mutableCopy);
}
-(NSMutableDictionary *)foundDataDic
{
    return LS_LAZY(_foundDataDic, @{}.mutableCopy);
}
@end
